package component.auxiliares.view;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import component.auxiliares.Campo;

import control.Supervisorio;
import entities.Variable;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class FieldConfig extends javax.swing.JFrame {
	private JTextField txtPre;
	private JComboBox cmbVariable;
	private JButton btnCancel;
	private JButton btnOK;
	private JSeparator jSeparator1;
	private JTextField txtPos;
	private Campo main;

	private Supervisorio supervisorio;
	private ArrayList<Variable> variables;

	/**
	 * Auto-generated main method to display this JFrame
	 */

	public FieldConfig(Campo campo) {
		super();
		supervisorio = Supervisorio.getInstance();

		main = campo;
		initGUI();
	}

	private void initGUI() {

		this.variables = supervisorio.getAllVariables();

		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("i2M - Campo");
			{
				ComboBoxModel cmbVariableModel = new DefaultComboBoxModel(this
						.getVariablesName().toArray());
				cmbVariable = new JComboBox();
				getContentPane().add(cmbVariable);
				cmbVariable.setModel(cmbVariableModel);
				cmbVariable.setBounds(67, 12, 164, 21);
			}
			{
				jSeparator1 = new JSeparator();
				getContentPane().add(jSeparator1);
				jSeparator1.setBounds(12, 45, 274, 10);
			}
			{
				btnOK = new JButton();
				getContentPane().add(btnOK);
				btnOK.setText("OK");
				btnOK.setBounds(12, 57, 92, 21);
				btnOK.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						btnOKMouseClicked(evt);
					}
				});
			}
			{
				btnCancel = new JButton();
				getContentPane().add(btnCancel);
				btnCancel.setText("Cancelar");
				btnCancel.setBounds(184, 57, 104, 21);
				btnCancel.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						btnCancelMouseClicked(evt);
					}
				});
			}

			setVariableInput();

			pack();
			this.setSize(306, 123);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setVariableInput() {

		if (main.getPojo().getVariable() != null) {
			for (int i = 0; i < variables.size(); i++) {

				if (variables.get(i).getId() == main.getPojo().getVariable()
						.getId()) {
					cmbVariable.setSelectedIndex(i);
					break;
				}
			}
		}

	}

	private ArrayList<String> getVariablesName() {

		ArrayList<String> names = new ArrayList<String>();

		for (Variable variable : variables) {
			names.add(variable.getName());
		}

		return names;

	}

	public JTextField getTxtPre() {
		return txtPre;
	}

	public JComboBox getCmbVariable() {
		return cmbVariable;
	}

	public JTextField getTxtPos() {
		return txtPos;
	}

	private void btnCancelMouseClicked(MouseEvent evt) {
		this.dispose();
	}

	private void btnOKMouseClicked(MouseEvent evt) {

		int index = cmbVariable.getSelectedIndex();
		Variable v = this.variables.get(index);

		main.getPojo().setVariable(v);

		dispose();
	}

}
